package FCTranscode;

use 5.008008;
use strict;
use warnings;
use POSIX 'strftime';
use Fcntl qw(:DEFAULT :flock);
require Exporter;

our @ISA = qw(Exporter);

# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.

# This allows declaration	use FCTranscode ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
	
) ] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(
                $settingsdir
                $outputdir
                $store_location
                $ffmpeg_status_dir
                $cache_dir

                $monitor_loc
                $conv_loc
                $mediainfo
                $collector_file

                $req_script

                $ffmpeg_status_files

                $local_host
                $storage_url
                $remote_host
                $user
                $pass
                $path

                write_log
                error_die
                quick_warn
                remove_item
);

our $VERSION = '0.01';


# Preloaded methods go here.

my $work_dir = '/Users/ecstv/fctranscode/';
our $settingsdir = $work_dir.'/settings_short/';
#our $outputdir = $work_dir.'output/'; # For testing
our $outputdir = '/Volumes/Server HD 2/Production Media/Media/';
our $store_location = '/Volumes/Server HD 2/Production Media/';
our $ffmpeg_status_dir = $work_dir.'status/ffmpeg/';
our $cache_dir = '/tmp/';


our $monitor_loc = $work_dir.'/monitor.pl';
our $conv_loc = $work_dir.'conv.pl';
our $mediainfo = '/usr/local/bin/mediainfo';
our $collector_file = $work_dir.'status/collector_status';
our $req_script = "/cgi-bin/get_requests.cgi";


our $ffmpeg_status_files = $ffmpeg_status_dir.'*.status';


our $local_host = 'director.ecs.soton.ac.uk';
our $storage_url = 'http://'.$local_host.'/videos/Media/';
our $remote_host = 'archive.zepler.tv';
our $user = 'admin';
our $pass = 'ecstv';
our $path = '/sword-app/deposit/inbox';

# Function to log and output warnings

my $log_file = $work_dir.'scripts.log';

sub write_log{
        my $message = shift;
        my $cur_time = strftime "%d %b %Y (%H:%M:%S)", localtime;
        $message = $cur_time.' '.$message;
        open(FH, ">>", $log_file) or die("Could not open log file ".$log_file.": $!");
        flock FH, LOCK_EX;
        print FH $message."\n";
        flock FH, LOCK_UN;
        close(FH);
}
sub quick_warn{

        #TODO - Filter the 80xx messages so it is not forwarded to the user

        my $message = shift;
	chomp($message);
        # TODO - Uncomment this chomp($message);
        write_log($message);
        print $message."\n";
}

# Function to log and output errors
#Add default "ERROR:" in the messages. Redo logs.
sub error_die{

        my $message = shift;
	
        # TODO: Uncomment this chomp($message);
        write_log($message);
        print $message."\n";
        exit(1);
}

sub remove_item{
        my $old_item = shift;
        chomp($old_item);
        print "REMOVING: $old_item\n";

        sysopen(FH, $collector_file, O_RDWR); # Check for failure
        flock FH, LOCK_EX;
        my @old_list = <FH>;
        foreach my $cur_item (@old_list){
                chomp($cur_item);
                #print "\n";
                #print "CUR:$cur_item\n";
                #print "OLD:$old_item\n";
                #print "\n\n";
                if ($cur_item ne $old_item) {print FH $cur_item."\n";}
        }
        flock FH, LOCK_UN;
        close FH;
}

1;
__END__
# Below is stub documentation for your module. You'd better edit it!

=head1 NAME

FCTranscode - Perl extension for blah blah blah

=head1 SYNOPSIS

  use FCTranscode;
  blah blah blah

=head1 DESCRIPTION

Stub documentation for FCTranscode, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.

Blah blah blah.

=head2 EXPORT

None by default.



=head1 SEE ALSO

Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.

If you have a mailing list set up for your module, mention it here.

If you have a web site set up for your module, mention it here.

=head1 AUTHOR

ECS-TV, E<lt>ecstv@apple.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009 by ECS-TV

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.


=cut
